package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	import utils.Button;
	import utils.Res;
	import utils.events.MenuEvent;

	public class DeskMenuItem
	{
		private var _DeskMenuItemName:String;
		private var _menubtn:Button;
		private var _btn_vedio:Button;
		private var _contentbitmap:Bitmap;
		private var _menuContainer:Sprite;
		private var _contentContainer:Sprite;
		private var menuContentX:Number=0;
		private var menuContentY:Number=221;
		public var boolShowContent:Boolean;
		private var _btn_desk_down:Bitmap;
		private var _btn_desk_up:Bitmap;
		private var _btn_DeskMenuContainer:Sprite;
		private var _btn_deskmenu_normal:Bitmap;
		private var _btn_deskmenu_over:Bitmap;

		public function DeskMenuItem(menuName:String, downUrl:Class, overUrl:Class, contentContainer:Sprite)
		{
			this._DeskMenuItemName=menuName;
			_menuContainer=new Sprite();
			_menubtn=new Button(downUrl, overUrl, null);
			_menubtn.setClickHandler(onBtnClickHandler);
			this._menuContainer.addChild(this._menubtn);
			//视频按钮
			_btn_vedio=new Button(Vedio_btn, Vedio_btn_over, null);
			_btn_vedio.x=438;
			_btn_vedio.y=(this._menubtn.height - _btn_vedio.height) / 2;
			this._menuContainer.addChild(this._btn_vedio);
			//桌子位置按钮
			_btn_DeskMenuContainer=new Sprite();
			_btn_DeskMenuContainer.x=276;
			_btn_DeskMenuContainer.addChild(new Bitmap(new Btn_DeskMenu_Bg as BitmapData)); //位置按钮
			_btn_deskmenu_normal=new Bitmap(new Btn_DeskMenu_Normal as BitmapData);
			_btn_DeskMenuContainer.addChild(_btn_deskmenu_normal); //位置正常按钮
			_btn_deskmenu_normal.x=44;
			_btn_deskmenu_normal.y=(this._btn_DeskMenuContainer.height - _btn_deskmenu_normal.height) / 2;
			_btn_deskmenu_over=new Bitmap(new Btn_DeskMenu_Over as BitmapData);
			_btn_DeskMenuContainer.addChild(_btn_deskmenu_over); //位置over按钮
			_btn_deskmenu_over.x=44;
			_btn_deskmenu_over.y=(this._btn_DeskMenuContainer.height - _btn_deskmenu_normal.height) / 2;
			_btn_deskmenu_over.visible=false;

			_btn_DeskMenuContainer.y=(this._menuContainer.height - _btn_DeskMenuContainer.height) / 2;
			this._menuContainer.addChild(this._btn_DeskMenuContainer);
			//初始化打开合起的指示图标

			_btn_desk_down=new Bitmap(new Btn_Desk_Down as BitmapData);
			_btn_desk_down.x=409;
			_btn_desk_down.y=this._menuContainer.height - _btn_desk_down.height - 2;
			this._menuContainer.addChild(this._btn_desk_down);

			_btn_desk_up=new Bitmap(new Btn_Desk_Up as BitmapData);
			_btn_desk_up.x=409;
			_btn_desk_up.y=this._menuContainer.height - _btn_desk_up.height - 2;
			_btn_desk_up.visible=false;
			this._menuContainer.addChild(this._btn_desk_up);
			if (contentContainer)
			{
				this._contentContainer=contentContainer;
			}
			else
			{
				_contentContainer=new Sprite();
			}

		}

		public function get btn_desk_up():Bitmap
		{
			return _btn_desk_up;
		}

		public function set btn_desk_up(value:Bitmap):void
		{
			_btn_desk_up=value;
		}

		public function get btn_desk_down():Bitmap
		{
			return _btn_desk_down;
		}

		public function set btn_desk_down(value:Bitmap):void
		{
			_btn_desk_down=value;
		}

		public function get menuContainer():Sprite
		{
			return _menuContainer;
		}

		public function set menuContainer(value:Sprite):void
		{
			_menuContainer=value;
		}

		public function get menubtn():Button
		{
			return _menubtn;
		}

		public function set menubtn(value:Button):void
		{
			_menubtn=value;
		}

		public function get contentContainer():Sprite
		{
			return _contentContainer;
		}

		public function set contentContainer(value:Sprite):void
		{
			_contentContainer=value;
		}


		public function get contentbitmap():Bitmap
		{
			return _contentbitmap;
		}

		public function set contentbitmap(value:Bitmap):void
		{
			_contentbitmap=value;
		}

		protected function onBtnClickHandler(event:MouseEvent):void
		{
			trace("before this._menuContainer.x:" + this._menuContainer.x + "y:" + this._menuContainer.y + " height:" + this._menuContainer.height);
			var menuArrays:Array=this._menuContainer.parent.parent.parent["menuArrays"] as Array;
			var index:Number=menuArrays.indexOf(this);
			var preMenu:DeskMenuItem=DeskMenuItem(this._menuContainer.parent.parent.parent["curentContent"]);
			if (preMenu != null && preMenu != this)
			{
				preMenu.boolShowContent=false;
				preMenu.btn_desk_up.visible=false;
				preMenu.btn_desk_down.visible=true;
				if (preMenu.menuContainer.contains(preMenu._contentContainer))
				{
					preMenu.menuContainer.removeChild(preMenu._contentContainer);
					var preindex:Number=menuArrays.indexOf(preMenu);
					for (var i:int=preindex + 1; i < menuArrays.length; i++)
					{
						DeskMenuItem(menuArrays[i]).menuContainer.y-=preMenu._contentContainer.height;
					}
				}

			}
			this._contentContainer.x=this._menuContainer.x-5;
			this._contentContainer.y=this._menuContainer.y + this._menubtn.height;

			//如果是显示，那么就合起
			if (boolShowContent)
			{
				if (this._menuContainer.contains(this.contentContainer))
				{
					this._menuContainer.removeChild(this.contentContainer);
				}
				this._btn_desk_up.visible=false;
				this._btn_desk_down.visible=true;
				//下面的菜单y-		
				for (var z:int=index + 1; z < menuArrays.length; z++)
				{
					DeskMenuItem(menuArrays[z]).menuContainer.y-=this._contentContainer.height;
				}

			}
			else
			{
				//下面的菜单y+
				if (!this._menuContainer.contains(this.contentContainer))
				{
					this.contentContainer.y=this._menuContainer.height;
					this._menuContainer.addChild(this.contentContainer);
				}
				this._btn_desk_up.visible=true;
				this._btn_desk_down.visible=false;
				for (var k:int=index + 1; k < menuArrays.length; k++)
				{
					DeskMenuItem(menuArrays[k]).menuContainer.y+=this._contentContainer.height;
				}
				var len_hidden:int=this._menuContainer.parent.y + this._menuContainer.y + this._menuContainer.height - 296;
				//如果超出部分大于容器底部
				len_hidden=len_hidden > 0 ? len_hidden : 0;
				this._menuContainer.parent.y-=len_hidden;
				/*
				for (var j:int=0; j < menuArrays.length; j++)
				{
					//下面部分
					if(j>=index){
						DeskMenuItem(menuArrays[j]).menuContainer.y+=this._contentContainer.height-len_hidden;
					}else{
						DeskMenuItem(menuArrays[j]).menuContainer.y-=len_hidden;
					}
				}*/
			}
			trace("after this._menuContainer.x:" + this._menuContainer.x + "y:" + this._menuContainer.y + " height:" + this._menuContainer.height);
			trace("after this.contentContainer.x:" + this.contentContainer.x + "y:" + this.contentContainer.y + " height:" + this.contentContainer.height);
			boolShowContent=!boolShowContent;
			this.menuContainer.parent.parent.parent["curentContent"]=this;
			this.menuContainer.parent.parent.dispatchEvent(new MenuEvent(MenuEvent.onMenuItemClick, ""));
		}

		protected function onBtnClickHandler1(event:MouseEvent):void
		{
			var menuArrays:Array=this._menuContainer.parent.parent.parent["menuArrays"] as Array;
			var index:Number=menuArrays.indexOf(this);
			var preMenu:DeskMenuItem=DeskMenuItem(this._menuContainer.parent.parent.parent["curentContent"]);
			if (preMenu != null && preMenu != this)
			{
				preMenu.boolShowContent=false;
				preMenu.btn_desk_up.visible=false;
				preMenu.btn_desk_down.visible=true;
				if (this._menuContainer.parent.parent.parent.contains(preMenu._contentContainer))
				{
					this._menuContainer.parent.parent.parent.removeChild(preMenu._contentContainer);
					var preindex:Number=menuArrays.indexOf(preMenu);
					for (var i:int=preindex + 1; i < menuArrays.length; i++)
					{
						DeskMenuItem(menuArrays[i]).menuContainer.y-=preMenu._contentContainer.height;
					}
				}

			}
			this._contentContainer.x=this._menuContainer.x;
			this._contentContainer.y=this._menuContainer.y + this._menubtn.height;

			//如果是显示，那么就合起
			if (boolShowContent)
			{
				this._menuContainer.parent.parent.parent.removeChild(this._contentContainer);
				this._btn_desk_up.visible=false;
				this._btn_desk_down.visible=true;
				//下面的菜单y-		
				for (var k:int=index + 1; k < menuArrays.length; k++)
				{
					DeskMenuItem(menuArrays[k]).menuContainer.y-=this._contentContainer.height;
				}
			}
			else
			{
				//下面的菜单y+
				this._menuContainer.parent.parent.parent.addChild(this._contentContainer);
				this._btn_desk_up.visible=true;
				this._btn_desk_down.visible=false;
				for (var j:int=index + 1; j < menuArrays.length; j++)
				{
					DeskMenuItem(menuArrays[j]).menuContainer.y+=this._contentContainer.height;
				}
			}
			boolShowContent=!boolShowContent;
			this.menuContainer.parent.parent.parent["curentContent"]=this;
			this.menuContainer.parent.parent.parent.dispatchEvent(new MenuEvent(MenuEvent.onMenuItemClick, ""));
		}

		public function get DeskMenuItemName():String
		{
			return _DeskMenuItemName;
		}

		public function set DeskMenuItemName(value:String):void
		{
			_DeskMenuItemName=value;
		}

	}
}
